import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Form, Tabs } from 'antd';
import classNames from 'classnames';
import LoginItem from './LoginItem';
import LoginTab from './LoginTab';
import LoginSubmit from './LoginSubmit';
import styles from './index.less';

class Login extends Component {
    static propTypes = {
        className: PropTypes.string,
        defaultActiveKey: PropTypes.string,
        onTabChange: PropTypes.func,
        onSubmit: PropTypes.func,
    };

    static childContextTypes = {
        tabUtil: PropTypes.object,
        form: PropTypes.object,
        updateActive: PropTypes.func,
        shouldBeInitialized: PropTypes.bool,
    };

    static defaultProps = {
        className: '',
        defaultActiveKey: '',
        onTabChange: () => {},
        onSubmit: () => {},
    };

    constructor(props) {
        super(props);
        this.state = {
            type: props.defaultActiveKey,
            tabs: [],
            active: {},
        };
    }

    getChildContext() {
        const { tabs } = this.state;
        const { form, shouldBeInitialized } = this.props;
        return {
            tabUtil: {
                addTab: id => {
                    this.setState({
                        tabs: [...tabs, id],
                    });
                },
                removeTab: id => {
                    this.setState({
                        tabs: tabs.filter(currentId => currentId !== id),
                    });
                },
            },
            form,
            shouldBeInitialized,
            updateActive: activeItem => {
                const { type, active } = this.state;
                if (active[type]) {
                    active[type].push(activeItem);
                } else {
                    active[type] = [activeItem];
                }
                this.setState({
                    active,
                });
            },
        };
    }

    onSwitch = type => {
        const { onTabChange } = this.props;
        this.setState({
            type,
        });
        // this.timer = setTimeout(() => {
        //     this.changeHandle();
        //     clearTimeout(this.timer);
        // });
        onTabChange(type);
    };
    /*
    changeHandle = () => {
        const { active, type } = this.state;
        const { form, onValiateChange } = this.props;
        const activeFileds = active[type];
        form.validateFields(activeFileds, { force: true }, (err, values) => {
            onValiateChange(err, values);
        });
    };
  */
    handleSubmit = e => {
        e.preventDefault();
        const { active, type } = this.state;
        const { form, onSubmit } = this.props;
        const activeFileds = active[type];
        form.validateFields(activeFileds, { force: true }, (err, values) => {
            onSubmit(err, values);
        });
    };

    render() {
        const { className, children } = this.props;
        const { type, tabs } = this.state;
        const TabChildren = [];
        const otherChildren = [];
        React.Children.forEach(children, item => {
            if (!item) {
                return;
            }
            // eslint-disable-next-line
            if (item.type.__ANT_PRO_LOGIN_TAB) {
                TabChildren.push(item);
            } else {
                otherChildren.push(item);
            }
        });
        return (
            <div className={classNames(className, styles.login)}>
                <Form onSubmit={this.handleSubmit}>
                    {tabs.length ? (
                        <div>
                            <Tabs
                                animated={false}
                                className={styles.tabs}
                                activeKey={type}
                                onChange={this.onSwitch}
                                renderTabBar={() => (<span
                                    style={{
                                        display: 'inline-block',
                                        fontSize: 16,
                                        marginBottom: '20px',
                                        marginTop: '-20px',
                                        marginLeft: '10px'
                                    }}
                                >账号密码登录</span>)}
                            >
                                {TabChildren}
                            </Tabs>
                            {otherChildren}
                        </div>
                    ) : (
                        [...children]
                    )}
                </Form>
            </div>
        );
    }
}

Login.Tab = LoginTab;
Login.Submit = LoginSubmit;
Object.keys(LoginItem).forEach(item => {
    Login[item] = LoginItem[item];
});

export default Form.create()(Login);
